package com.one.group.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.ComplaintManagementAddDto;
import com.one.group.model.dto.ComplaintManagementQueryDto;
import com.one.group.model.dto.ComplaintManagementUpdateDto;
import com.one.group.model.dto.UpdateComplaintStatusDto;
import com.one.group.model.vo.ComplaintManagementListVo;
import com.one.group.result.Result;
import com.one.group.service.ComplaintManagementService;
import com.one.group.utils.ResultUtil;
import com.one.group.utils.ThrowUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 投诉管理控制器
 * @author
 * @date
 * @description
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/complaintManagement")
@Tag(name = "投诉管理接口")
@Slf4j
public class ComplaintManagementController {

    @Resource
    private ComplaintManagementService complaintManagementService;

    @PostMapping("/list")
    @Operation(summary = "获取投诉列表")
    public Result list(@RequestBody ComplaintManagementQueryDto complaintManagementQueryDto) {
        // 获取租户ID
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id:"));
        complaintManagementQueryDto.setTenantId(tenantId);
        
        // 设置分页条件
        PageHelper.startPage(complaintManagementQueryDto.getPage(), complaintManagementQueryDto.getLimit());
        
        // 查询数据
        List<ComplaintManagementListVo> list = complaintManagementService.findComplaintManagementByQueryDto(complaintManagementQueryDto);
        
        // 封装分页结果
        PageInfo<ComplaintManagementListVo> pageInfo = new PageInfo<>(list);
        
        return ResultUtil.success(pageInfo);
    }

    @GetMapping("/find")
    @Operation(summary = "根据ID查询投诉详情")
    public Result find(@RequestParam Long id) {
        ThrowUtil.throwIf(id == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        return ResultUtil.success(complaintManagementService.getComplaintManagementById(id));
    }

    @PostMapping("/add")
    @Operation(summary = "添加投诉记录")
    public Result add(@RequestBody ComplaintManagementAddDto complaintManagementAddDto) {
        ThrowUtil.throwIf(BeanUtil.isEmpty(complaintManagementAddDto), ErrorCode.PARAMS_ERROR, "参数不能为空");
        // 获取租户ID
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id:"));
        complaintManagementAddDto.setTenantId(tenantId);
        
        if (complaintManagementService.addComplaintManagement(complaintManagementAddDto)) {
            return ResultUtil.success("添加成功");
        } else {
            return ResultUtil.error(ErrorCode.OPERATION_ERROR);
        }
    }

    @PutMapping("/update")
    @Operation(summary = "更新投诉记录")
    public Result update(@RequestBody ComplaintManagementUpdateDto complaintManagementUpdateDto) {
        ThrowUtil.throwIf(BeanUtil.isEmpty(complaintManagementUpdateDto), ErrorCode.PARAMS_ERROR, "参数不能为空");
        // 获取租户ID
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id:"));
        complaintManagementUpdateDto.setTenantId(tenantId);
        
        if (complaintManagementService.updateComplaintManagement(complaintManagementUpdateDto)) {
            return ResultUtil.success("更新成功");
        } else {
            return ResultUtil.error(ErrorCode.OPERATION_ERROR);
        }
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除投诉记录")
    public Result delete(@RequestParam Long id) {
        ThrowUtil.throwIf(id == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        if (complaintManagementService.deleteComplaintManagement(id)) {
            return ResultUtil.success("删除成功");
        } else {
            return ResultUtil.error(ErrorCode.OPERATION_ERROR);
        }
    }
    
    @PostMapping("/accept")
    @Operation(summary = "受理投诉")
    public Result accept(@RequestBody UpdateComplaintStatusDto updateComplaintStatusDto) {
        ThrowUtil.throwIf(BeanUtil.isEmpty(updateComplaintStatusDto), ErrorCode.PARAMS_ERROR, "参数不能为空");
        ThrowUtil.throwIf(updateComplaintStatusDto.getId() == null, ErrorCode.PARAMS_ERROR, "投诉ID不能为空");
        
        if (complaintManagementService.acceptComplaint(updateComplaintStatusDto)) {
            return ResultUtil.success("受理成功");
        } else {
            return ResultUtil.error(ErrorCode.OPERATION_ERROR);
        }
    }

    
}